#Include "Protheus.ch"

User Function ContPed()

Local oReport := ReportDef()

oReport:PrintDialog()

Return Nil

// ------------------------------------------------------------------------------------------------------- //

Static Function ReportDef()

Local aSecTab01 := {"SC5", "SA1"}
Local aSecTab02 := {"SC6", "SB1"}

Local bAction    := {|oReport| ReportPrint(oReport)}

Local cDescript  := "Este programa tem como objetivo imprimir relatorio de acordo com os parametros informados pelo usuario."
Local cReport    := "CONTPED"
Local cTitle     := "Contagem x Pedido"
Local cPerg      := "XCXCXC"
Local cTitSec01  := "Cliente"
Local cTitSec02  := "Produtos"

Local oReport    := Nil
Local oSection1  := Nil
Local oSection2  := Nil
Local oBreak     := Nil

oReport := TReport():New(cReport, cTitle, cPerg, bAction, cDescript)

oReport:SetPortrait()

oSection1 := TRSection():New(oReport, cTitSec01, aSecTab01)

TRCell():New(oSection1, "C5_NUM"    , "SC5")
TRCell():New(oSection1, "C5_CLIENTE", "SC5")
TRCell():New(oSection1, "C5_LOJACLI", "SC5")
TRCell():New(oSection1, "A1_NOME"   , "SA1")
TRCell():New(oSection1, "A1_END"    , "SA1")
TRCell():New(oSection1, "A1_MUN"    , "SA1")

oSection2 := TRSection():New(oSection1, cTitSec02, aSecTab02)

TRCell():New(oSection2, "C6_PRODUTO", "SC6")
TRCell():New(oSection2, "B1_DESC"   , "SB1")
TRCell():New(oSection2, "C6_UNSVEN" , "SC6")
TRCell():New(oSection2, "C6_QTDBRU" , "SC6")
TRCell():New(oSection2, "C6_QTDVEN" , "SC6")
TRCell():New(oSection2, "C6_QTDPCT" , "SC6")

Return (oReport)

// ----------------------------------------------------------------------------------------------- //

Static Function ReportPrint(oReport)

Local cAlias    := GetNextAlias()

Local oSection1 := oReport:Section(1)
Local oSection2 := oSection1:Section(1)
Local oBreak    := Nil

MakeSqlExpr(oReport:uParam)

oSection1:BeginQuery()

	BeginSql Alias cAlias
	
		SELECT C5_NUM, C6_NUM, C5_CLIENTE, C5_LOJACLI, A1_NOME, A1_END, A1_MUN,
			C6_PRODUTO, B1_DESC, C6_UNSVEN, C6_QTDBRU, C6_QTDVEN, C6_QTDPCT
		FROM %Table:SC5% SC5
			INNER JOIN %Table:SA1% SA1
				ON C5_CLIENTE = A1_COD AND C5_LOJACLI = A1_LOJA
			INNER JOIN %Table:SC6% SC6
				ON C6_NUM = C5_NUM
			INNER JOIN %Table:SB1% SB1
				ON B1_COD = C6_PRODUTO
		WHERE C5_AGREG BETWEEN %Exp:MV_PAR01% AND %Exp:MV_PAR02%
			AND C5_QUEIJOS BETWEEN %Exp:MV_PAR03% AND %Exp:MV_PAR04%
			AND C6_PRODUTO <> %Exp:'315500'%
			AND C6_PRODUTO <> %Exp:'310000'%
			AND B1_GRUPO = %Exp:'3'%
			AND SC5.C5_FILIAL = %xFilial:SC5%
			AND SC6.C6_FILIAL = %xFilial:SC6%
			AND SB1.B1_FILIAL = %xFilial:SB1%
			AND SA1.A1_FILIAL = %xFilial:SA1%
			AND SC5.%NotDel%
			AND SC6.%NotDel%
			AND SA1.%NotDel%
			AND SB1.%NotDel%
	
	EndSql
	
//	MemoWrite("C:\RelMicrosiga\Qry.txt", GetLastQuery()[2])

	oSection2:SetParentQuery()
	oSection2:SetParentFilter({|cParam| (cAlias)->C6_NUM == cParam }, {|| (cAlias)->C5_NUM})

oSection1:EndQuery()

oBreak := TRBreak():New(oSection1, oSection1:Cell("C5_NUM"), "Totais:")

TRFunction():New(oSection2:Cell("C6_PRODUTO"), , "COUNT", oBreak, , , , .F.)
TRFunction():New(oSection2:Cell("C6_UNSVEN" ), , "SUM"  , oBreak, , , , .F.)
TRFunction():New(oSection2:Cell("C6_QTDBRU" ), , "SUM"  , oBreak, , , , .F.)
TRFunction():New(oSection2:Cell("C6_QTDVEN" ), , "SUM"  , oBreak, , , , .F.)
TRFunction():New(oSection2:Cell("C6_QTDPCT" ), , "SUM"  , oBreak, , , , .F.)

oSection1:ForceLineStyle(.T.)
oSection1:PageBreak(.T.)

oSection1:Print()

Return NIL